package 
{
	import Common.*;
	
	import Events.*;
	
	import flash.display.*;
	import flash.events.Event;
	import flash.system.Security;
	import flash.utils.ByteArray;
	
	
	
	
	public class LoginLib extends Sprite implements GameModule
	{
		private var uiSprite:Sprite = null;
		private var loginUIHandler:LoginUIHandler = null;
		
		[Embed(source="./lib/role.xml" , mimeType="application/octet-stream")]
		public var roleClass:Class;
		
		[Embed(source="../lib/default.png") ]
		public static var defaultClass:Class;
		
		[Embed(source="./lib/pal1.pal" , mimeType="application/octet-stream")]
		public var palClass:Class;
		
		
		public function LoginLib()
		{
			
		}
		
		
		public function init():Boolean
		{
			MessageBox.uiMc = new MsgUI();
			MessageBox.init();
			
			ActionManager.instance.init( new defaultClass() , new palClass() );
			RoleConfig.instance.init( XML( new roleClass() ) );
			
			ModuleEventManager.instance.addEventListener( ModuleEvent.INIT , onInit );
			ModuleEventManager.instance.addEventListener( ModuleEvent.LOGINRELEASE , onRelease );
			
			ModuleEventManager.instance.addEventListener( ModuleEvent.MAINRELEASE , onMainRelease );
			
			return true;
		}
		
		
		public function release():void
		{
			
		}
		
		
		/////////////////////////////////////////////////////////////////
		
		
		private function onMainRelease( event:ModuleEvent ):void
		{
			RenderManager.instance.UISprite.addChild( uiSprite );
			
			loginUIHandler = new LoginUIHandler();
			loginUIHandler.init( uiSprite );
			
			MessageBox.Msg( GameDefine.STR_ConnectDown );
			
			ModuleEventManager.instance.addEventListener( ModuleEvent.RESIZE, resize );
		
			resize( null );		
		}
		
		
		private function resize( e:ModuleEvent ):void
		{
			uiSprite.graphics.beginFill( 0x000000 );
			uiSprite.graphics.drawRect( 0 , 0 , RenderManager.instance.stageWidth , RenderManager.instance.stageHeight );
			uiSprite.graphics.endFill();
			
			loginUIHandler.resize();
		}
		
		
		private function onInit( event:ModuleEvent ):void
		{
			uiSprite = new Sprite();
			RenderManager.instance.UISprite.addChild( uiSprite );
			
			loginUIHandler = new LoginUIHandler();
			loginUIHandler.init( uiSprite );
			
			ModuleEventManager.instance.addEventListener( ModuleEvent.RESIZE, resize );
			
			resize( null );
		}
		
		
		private function onRelease( event:ModuleEvent ):void
		{
			loginUIHandler.release();
			loginUIHandler = null;
			
			RenderManager.instance.UISprite.removeChild( uiSprite );
			
			ModuleEventManager.instance.removeEventListener( ModuleEvent.RESIZE, resize );
		}
		
		
	}
}
